﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Xml;
namespace pcsse.WebService
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://sse.tongji.edu.cn/spb/rss")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            DataTable dt = db_news.getDBhandle().GetDataTableBySQL("SELECT TOP 10 * FROM T_NEWS LEFT JOIN T_CATEGORY ON T_NEWS.CATEGORY_ID = T_CATEGORY.CATEGORY_ID WHERE PERMISSION = 1 ORDER BY NEWS_ID DESC");

            System.IO.MemoryStream newslist = new System.IO.MemoryStream();


            XmlTextWriter writer = new XmlTextWriter(newslist, System.Text.Encoding.UTF8);

            writer.WriteStartDocument();
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "同济大学软件学院党支部信息发布");
            writer.WriteElementString("link", "http://sse.tongji.edu.cn/spb/");
            writer.WriteElementString("description", "发布最新十条同济大学软件学院党支部的最新信息");
            writer.WriteElementString("language", "zh-cn");
            writer.WriteElementString("lastBuildDate", DateTime.Now.ToString());
            writer.WriteElementString("docs", "http://blogs.law.harvard.edu/tech/rss");
            writer.WriteElementString("generator", "ASP.NET");
            writer.WriteElementString("managingEditor", "homezhangdi@yahoo.com.cn");
            writer.WriteElementString("webMaster", "281821301@qq.com");
            foreach (DataRow dr in dt.Rows)
            {

                writer.WriteStartElement("item");
                writer.WriteElementString("title", dr["TITLE"].ToString());
                writer.WriteElementString("link", "http://sse.tongji.edu.cn/spb/News/news_read.aspx?id="+dr["NEWS_ID"].ToString());
                if (dr["TEXT_CONTENT"].ToString().Length > 0)
                    writer.WriteElementString("description", dr["TEXT_CONTENT"].ToString().Substring(0, dr["TEXT_CONTENT"].ToString().IndexOf("</p>") + 4));
                else
                    writer.WriteElementString("description", "");
                writer.WriteElementString("pubDate", dr["Date"].ToString());
                writer.WriteElementString("category", dr["CATEGORY_TITLE"].ToString());
                writer.WriteElementString("guid", "http://sse.tongji.edu.cn/spb/News/news_read.aspx?id=" + dr["NEWS_ID"].ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
            
            context.Response.ContentType = "text/plain";
            context.Response.Write(System.Text.Encoding.UTF8.GetString(newslist.ToArray()));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
